#ifndef THUNDERRAID_GAMEITEM_H
#define THUNDERRAID_GAMEITEM_H

#include <QObject>
#include <QRect>
#include <QPixmap>
#include <QFlags>

class QPainter;
class GameView;

class GameItem : public QObject{
    Q_OBJECT
public:
    explicit GameItem(GameView *parent = nullptr);

    virtual void setPos(QPointF new_pos);
    virtual QPointF getPos();

    virtual void setSpeed(QPointF speed);
    virtual QPointF getSpeed();

    virtual void setBoundingSize(QSizeF sizeF);
    virtual QSizeF getBoundingSize();
    virtual QRectF getBoundingRect();

    bool collidesWithItem(GameItem *other);
    GameView *getView();
    void setView(GameView *view);

    /**
     * 更新游戏物品信息
     * @param interval_ms 时间间隔（毫秒）
     */
    virtual void update(qreal interval_ms);
    /**
     * 绘制游戏物品
     * @param painter 绘图器，由上级组件传入。
     */
    virtual void paint(QPainter *painter) = 0;
    QString type;

private:
    QPointF speed{};
    QPointF pos{};
    QSizeF boundingSize{};
    GameView *view;
};

class GamePixmapItem : public GameItem{
    Q_OBJECT
public:
    explicit GamePixmapItem(const QPixmap &pixmap, GameView *parent = nullptr);

    virtual void setPixmapRectF(QRectF rectF);
    virtual QRectF getPixmapRectF();
    void paint(QPainter *painter) override;

protected:
    QPixmap pixmap;

private:
    QRectF pixmap_rectF;
};

class GamePixmapMovieItem: public GameItem{
    Q_OBJECT
public:
    enum State { NotRunning, Paused, Running };
    GamePixmapMovieItem(QList<QPixmap> pixSequence, qreal time, GameView *view = nullptr);

    virtual void start();
    virtual void pause();
    virtual void reset();

    void update(qreal interval_ms) override;
    void paint(QPainter *painter) override;
signals:
    void finished();
private:
    State state;
    qreal interval_count{};
    qreal interval{};
    int currentFrameIndex{};
    QList<QPixmap> pixSequence;
};

#endif//THUNDERRAID_GAMEITEM_H
